package cn.shutdown.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import cn.shutdown.util.MessageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cn.shutdown.user.po.User;
import cn.shutdown.user.service.IUserService;
import cn.shutdown.util.Md5Util;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class LoginController {

	@Autowired
	private IUserService userService;

	/**
	 * 登录页面
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/login/loginPage.do")
	public ModelAndView loginPage(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		MessageUtil.buildMessage(request);
		return mav;
	}

	/**
	 * 从登录页面获取form表单的属性，然后判断并跳转
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/login/login.do")
	public ModelAndView login(Model model, @ModelAttribute User user, HttpSession session, RedirectAttributes attr) {
		ModelAndView mav = new ModelAndView();
		user.setPassword(Md5Util.encryptionUpperCase(user.getPassword()));
		User matchUser = userService.loginUser(user);
		if(matchUser != null){
			if("admin".equals(matchUser.getUserName())){
				mav.setViewName("redirect:/login/adminMain.do");
			}else{
				mav.setViewName("redirect:/login/main.do");
			}
			session.setAttribute("loginUser", matchUser);
		}else{
			mav.setViewName("redirect:/login/loginPage.do");
			MessageUtil.buildRedirectMessage(attr, MessageUtil.TYPE_WARN, "用户名或密码错误");
		}
		return mav;
	}

	/**
	 * 注册页面在
	 * @return
	 */
	@RequestMapping(value = "/login/registPage.do")
	public ModelAndView registPage() {
		ModelAndView mav = new ModelAndView();
		return mav;
	}

	/**
	 * 注册 方法
	 * @param attributes
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/login/regist.do", method = RequestMethod.POST)
	public ModelAndView regist(@ModelAttribute User user, RedirectAttributes attributes) {
		user.setPassword(Md5Util.encryptionUpperCase(user.getPassword()));
		userService.registUser(user);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:/login/loginPage.do");
		MessageUtil.buildRedirectMessage(attributes, MessageUtil.TYPE_MESSAGE, "注册成功，请重新登录");
		return mav;
	}
	
	/**
	 * 主页
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/login/main.do")
	public ModelAndView toMainPage(HttpSession session, HttpServletRequest request){
		ModelAndView mav = new ModelAndView();
		User matchUser = (User) session.getAttribute("loginUser");
		request.setAttribute(MessageUtil.PARAM_RESULT, MessageUtil.buildMessageMap(MessageUtil.TYPE_MESSAGE, "欢迎光临 " + matchUser.getName()));
		return mav;
	}
	
	/**
	 * 管理员主页
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/login/adminMain.do")
	public ModelAndView toAdminMainPage(HttpSession session, HttpServletRequest request){
		ModelAndView mav = new ModelAndView();
		User matchUser = (User) session.getAttribute("loginUser");
		request.setAttribute(MessageUtil.PARAM_RESULT, MessageUtil.buildMessageMap(MessageUtil.TYPE_MESSAGE, "欢迎光临 ,管理员" + matchUser.getName()));
		return mav;
	}
	
	/**
	 * 退出登录
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/login/logout.do")
	public ModelAndView logout(HttpSession session){
		session.removeAttribute("loginUser");
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:/login/loginPage.do");
		return mav;
	}
}
